<?php
require_once dirname(__FILE__) . '/../config/config.php';
class GameLinkController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function listAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $seo = new SEO();
        $gameCategoryService = new GameCategoryService();
        $deviceArr = $gameService ->getFullDevice($context, $result);
        $pager = new Pager();
        
        $curpage = isset ($_GET['page']) ? $_GET['page'] : 1;
    	if(isset($_GET['database']) && !empty($_GET['database']))
        {
        	$data = $_GET['database'];
        	if($data == 'tpg')
        	{	
        		$gameCategoryArr = $gameCategoryService ->getFullGameCategoryTPG($context, $result);
        		$isGameArr = $gameService ->getGameByIsGame($context, $result);
        		if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
	        		{
		        		$gameLinkArr = $gameService ->getGameLinkByGameCateIdTPG($context, $result, $filter);
		        		$lastStr = "controller=gameLink&action=list&database=tpg&filter=filterParent";
	        		}
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
	        			$gameLinkArr = $gameService ->getGameLinkByGameIdTPG($context, $result, $filter);
		        		$lastStr = "controller=gameLink&action=list&database=nsgt&filter=filterParent";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
        		else 
        		{
        			$count = $gameService->countGameLinkTPG($context, $result);
	        		$limit = 50;
	        		$start = $pager->findStart($limit);
	        		$pages = $pager->findPages($count, $limit);
	        		$gameLinkArr = $gameService -> getAllGameLinkByLimitTPG($context, $result, $start, $limit);
	        		$lastStr = "controller=gameLink&action=list&database=tpg";
        		}
        		$this->smarty->assign('isGameArr', $isGameArr);
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
        	}
	        else if($data == 'mobile')
	        {
	        	$gameCategoryArr = $gameCategoryService ->getFullGameCategoryMobile($context, $result);
	        	//$isGameArr = $gameService ->getGameByIsGame($context, $result);
        		if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
	        		{
		        		$gameLinkArr = $gameService ->getGameLinkByGameCateIdMobile($context, $result, $filter);
		        		$lastStr = "controller=gameLink&action=list&database=mobile&filter=filterParent";
	        		}
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
	        			$gameLinkArr = $gameService ->getGameLinkByGameIdMobile($context, $result, $filter);
		        		$lastStr = "controller=gameLink&action=list&database=mobile&filter=filterParent";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
        		else 
        		{
		        	$count = $gameService->countGameLinkMobile($context, $result);
		        	$limit = 30;
		        	$start = $pager->findStart($limit);
		        	$pages = $pager->findPages($count, $limit);
		        	$gameLinkArr = $gameService->getAllGameLinkByLimitMobile($context, $result, $start, $limit);
		        	$lastStr = "controller=gameLink&action=list&database=mobile";
        		}
        		//$this->smarty->assign('isGameArr', $isGameArr);
        		$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
	        }
	        
	        else if($data == 'mmbg')
	        {
	        	$gameCategoryArr = $gameCategoryService ->getFullGameCategoryMMBG($context, $result);
	        	$isGameArr = $gameService ->getGameByIsGame($context, $result);
        		if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
	        		{
		        		$gameLinkArr = $gameService ->getGameLinkByGameCateIdMMBG($context, $result, $filter);
		        		$lastStr = "controller=gameLink&action=list&database=mmbg&filter=filterParent";
	        		}
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
	        			$gameLinkArr = $gameService ->getGameLinkByGameIdMMBG($context, $result, $filter);
		        		$lastStr = "controller=gameLink&action=list&database=nsgt&filter=filterGame";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
        		else 
        		{
		        	$count = $gameService->countGameLinkMMBG($context, $result);
		        	$limit = 30;
		        	$start = $pager->findStart($limit);
		        	$pages = $pager->findPages($count, $limit);
		        	$gameLinkArr = $gameService->getAllGameLinkByLimitMMBG($context, $result, $start, $limit);
		        	$lastStr = "controller=gameLink&action=list&database=mmbg";
        		}
        		$this->smarty->assign('isGameArr', $isGameArr);
        		$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
        	}
        	$this->smarty->assign('data', $data);
       	}
       	else 
       	{
       		$isGameArr = $gameService ->getGameByIsGame($context, $result);
       		$gameCategoryArr = $gameCategoryService ->getFullGameCategoryTPG($context, $result);
       		$count = $gameService->countGameLinkTPG($context, $result);
        	$limit = 30;
        	$start = $pager->findStart($limit);
        	$pages = $pager->findPages($count, $limit);
        	$gameLinkArr = $gameService->getAllGameLinkByLimitTPG($context, $result, $start, $limit);
       		$lastStr = "controller=gameLink&action=list&database=tpg";
       		$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
       		$this->smarty->assign('isGameArr', $isGameArr);
       	}
       	if(isset($pager) && isset($pages) && isset($lastStr))
        {
	        $pageFirst = $pager->findFirst($curpage, $pages, $lastStr);
	        $pagePrev = $pager->findPrev($curpage, $pages, $lastStr);
	        $pageList = $pager->pageList($curpage, $pages, $lastStr);
	        $pageNext = $pager->findNext($curpage, $pages, $lastStr);
	        $pageLast = $pager->findLast($curpage, $pages, $lastStr);
	
	        $this->smarty->assign('pageFirst', $pageFirst);
	        $this->smarty->assign('pagePrev', $pagePrev);
	        $this->smarty->assign('pageList', $pageList);
	        $this->smarty->assign('pageNext', $pageNext);
	        $this->smarty->assign('pageLast', $pageLast);
        }
        if(isset($gameLinkArr))
        {
        	$this->smarty->assign('gameLinkArr', $gameLinkArr);
        }
        $this->smarty->assign('deviceArr',$deviceArr);
        $this->smarty->display('list-game-link.tpl');
    }

    public function addAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();
        
        $isGameArr = $gameService ->getGameByIsGame($context, $result);
        $gameCategoryArr = $gameCategoryService->getFullGameCategory($context, $result);
		$deviceArr = $gameService ->getFullDevice($context, $result);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
            $gameService->insertGameLink($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list");
        }
        else
        {
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            $this->smarty->assign('isGameArr', $isGameArr);
            $this->smarty->assign('deviceArr', $deviceArr);
            $this->smarty->display('add-game-link.tpl');
        }
    }
    
	public function addMMBGAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();
        
        $isGameArr = $gameService ->getGameByIsGame($context, $result);
        $gameCategoryArr = $gameCategoryService->getFullGameCategoryMMBG($context, $result);
		$deviceArr = $gameService ->getFullDevice($context, $result);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
            $gameService->insertGameLinkMMBG($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=mmbg");
        }
        else
        {
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            $this->smarty->assign('isGameArr', $isGameArr);
            $this->smarty->assign('deviceArr', $deviceArr);
            $this->smarty->display('add-game-link-mmbg.tpl');
        }
    }
    
	public function addMobileAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();
        
        $gameCategoryArr = $gameCategoryService->getFullGameCategoryMobile($context, $result);
		$deviceArr = $gameService ->getFullDevice($context, $result);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
            $gameService->insertGameLinkMobile($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=mobile");
        }
        else
        {
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
      
            $this->smarty->assign('deviceArr', $deviceArr);
            $this->smarty->display('add-game-link-mobile.tpl');
        }
    }

    public function updateAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();      
        
        if(isset($_GET['database']) && !empty($_GET['database']))
		{
			$data = $_GET['database'];
			if($data == 'tpg')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
		        $isGameArr = $gameService ->getGameByIsGame($context, $result);
        		$gameCategoryArr = $gameCategoryService->getFullGameCategory($context, $result);
				$deviceArr = $gameService ->getFullDevice($context, $result);
				$gameLink = $gameService -> getGameLinkByIDTPG($context, $result, $id);
				
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);;
		        $this->smarty->assign('isGameArr', $isGameArr);
		        $this->smarty->assign('deviceArr', $deviceArr);
		        $this->smarty->assign('gameLink', $gameLink);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameService->updateGameLinkTPG($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=tpg");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-link.tpl');
			}
			elseif ($data == 'cgmb')
			{
				if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
		        $isGameArr = $gameService ->getGameByIsGame($context, $result);
        		$gameCategoryArr = $gameCategoryService->getFullGameCategoryCGMB($context, $result);
				$deviceArr = $gameService ->getFullDeviceCGMB($context, $result);
				$gameLink = $gameService -> getGameLinkByIDCGMB($context, $result, $id);
				
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);;
		        $this->smarty->assign('isGameArr', $isGameArr);
		        $this->smarty->assign('deviceArr', $deviceArr);
		        $this->smarty->assign('gameLink', $gameLink);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameService->updateGameLinkCGMB($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=cgmb");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-link.tpl');
			}
			elseif ($data == 'mmbg')
			{
				if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
		        $isGameArr = $gameService ->getGameByIsGame($context, $result);
        		$gameCategoryArr = $gameCategoryService->getFullGameCategoryMMBG($context, $result);
				$deviceArr = $gameService ->getFullDeviceMMBG($context, $result);
				$gameLink = $gameService -> getGameLinkByIDMMBG($context, $result, $id);
				
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);;
		        $this->smarty->assign('isGameArr', $isGameArr);
		        $this->smarty->assign('deviceArr', $deviceArr);
		        $this->smarty->assign('gameLink', $gameLink);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameService->updateGameLinkMMBG($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=mmbg");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-link.tpl');
			}
			elseif ($data == 'nsgt')
			{
				if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
		        $isGameArr = $gameService ->getGameByIsGame($context, $result);
        		$gameCategoryArr = $gameCategoryService->getFullGameCategoryNSGT($context, $result);
				$deviceArr = $gameService ->getFullDeviceNSGT($context, $result);
				$gameLink = $gameService -> getGameLinkByIDNSGT($context, $result, $id);
				
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);;
		        $this->smarty->assign('isGameArr', $isGameArr);
		        $this->smarty->assign('deviceArr', $deviceArr);
		        $this->smarty->assign('gameLink', $gameLink);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameService->updateGameLinkNSGT($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=nsgt");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-link.tpl');
			}
			
			$this->smarty->assign('data',$data);
		}

    }

    public function deleteAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameLinkService = new GameMessageService();
    	if(isset($_GET['database']) && !empty($_GET['database']))
		{
			$data = $_GET['database'];
			if($data == 'tpg')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameLinkService->deleteGameLinkTPG($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=tpg");
			}
			
			if($data == 'mobile')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameLinkService->deleteGameLinkMobile($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=mobile");
			}
			if($data == 'mmbg')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameLinkService->deleteGameLinkMMBG($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameLink&action=list&database=mmbg");
			}
		}
    }
    
	
}

?>